package com.project.common.core.utils.exportView.convert;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 日期类型转化
 */
public class DateConvert implements Convert {

    /**
     * 日志类
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private static final FastDateFormat FDF = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    private FastDateFormat outFDF = null;

    public DateConvert() {
        this(null);
    }

    public DateConvert(String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        outFDF = FastDateFormat.getInstance(pattern);
    }

    @Override
    public String convert(String value) {
        try {
            if (StringUtils.isNotBlank(value)) {
                return outFDF.format(FDF.parse(value));
            }
        } catch (ParseException e) {
            log.error("\r\n *********时间:{}----转化异常:{}", value, ExceptionUtils.getStackTrace(e));
        }
        return "";
    }


    public static void main(String[] args) {
        SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            outSDF.format(outSDF.parse("2020-03-04 15:01:01"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
